package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.*;
import com.qf.service.DtsUserService;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author luo
 * 用户管理及其子管理
 */
@RestController
@RequestMapping("/admin")
@CrossOrigin
public class UserController {

    @Autowired
    private DtsUserService dtsUserService;

    /**
     * 会员管理之会员列表
     * 接口地址 : http://localhost:8083/admin/user/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "查询")
    @RequiresPermissions(value = "admin:user:list")
    @GetMapping("/user/list")
    public Object userOfList(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "20") Integer limit, String sort, String order, String username,String mobile) {
        IPage<DtsUser> iPage = dtsUserService.findUserListByUsernameAndMobile(page, limit, sort, order, username,mobile);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 会员管理之会员审批
     * 接口地址 : http://localhost:8083/admin/user/approveAgency
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "会员审批")
    @RequiresPermissions(value = "admin:user:approveAgency")
    @PostMapping("/user/approveAgency")
    public Object userOfApproveAgency(@RequestBody String body) {
        //获取用户id，修改对应用户的status和level，佣金比例
        Integer settlementRate = JacksonUtil.parseInteger(body, "settlementRate");
//        System.out.println(settlementRate);
        Integer userId = JacksonUtil.parseInteger(body, "userId");
        if (!StringUtils.isEmpty(settlementRate)) {
            dtsUserService.modifyUserLevelAndRate(userId, settlementRate);
        }
        return ResponseUtil.ok();
    }

    /**
     * 会员管理之会员推广代理
     * 接口地址 : http://localhost:8083/admin/user/detailApprove?id=71
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员管理"}, button = "会员推广代理")
    @RequiresPermissions(value = "admin:user:detailApprove")
    @GetMapping("/user/detailApprove")
    public Object userOfDetailApprove(Integer id) {
        return ResponseUtil.ok(dtsUserService.findDetailApproveById(id));
    }

    /**
     * 佣金管理之佣金列表
     * 接口地址 : http://localhost:8083/admin/brokerage/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button = "查询")
    @RequiresPermissions(value = "admin:brokerage:list")
    @GetMapping("/brokerage/list")
    public Object brokerageOfList(Integer page, Integer limit, String sort, String order, String username,Integer mobile,Integer[] statusArray) {
        IPage<DtsAccountTrace> iPage = dtsUserService.findBrokerageListByQuery(page, limit, sort, order, username,mobile,statusArray);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("traceList", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 佣金管理之佣金审批
     * 接口地址 : http://localhost:8083/admin/brokerage/approve
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "佣金管理"}, button = "编辑")
    @RequiresPermissions(value = "admin:brokerage:approve")
    @PostMapping("/brokerage/approve")
    public Object brokerageOfApprove(@RequestBody String body) {
        Integer id = JacksonUtil.parseInteger(body, "id");
        Integer status = JacksonUtil.parseInteger(body, "status");
        String traceMsg = JacksonUtil.parseString(body, "traceMsg");
        dtsUserService.modifyBrokerageByUserId(id,status,traceMsg);
        return ResponseUtil.ok();
    }

    /**
     * 收货地址之收货地址列表
     * 接口地址 : http://localhost:8083/admin/address/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "收货地址"}, button = "查询")
    @RequiresPermissions(value = "admin:address:list")
    @GetMapping("/address/list")
    public Object addressOfList(Integer page, Integer limit, String sort, String order, String name, Integer userId) {
        IPage<DtsAddress> iPage = dtsUserService.findAddressListByQuery(page, limit, sort, order, name, userId);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 会员收藏之会员收藏列表
     * 接口地址 : http://localhost:8083/admin/collect/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员收藏"}, button = "查询")
    @RequiresPermissions(value = "admin:collect:list")
    @GetMapping("/collect/list")
    public Object collectOfList(Integer page, Integer limit, String sort, String order, Integer userId, Integer valueId) {
        IPage<DtsCollect> iPage = dtsUserService.findCollectListByQuery(page, limit, sort, order, userId, valueId);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * 会员足迹之会员足迹列表
     * 接口地址 : http://localhost:8083/admin/footprint/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "会员足迹"}, button = "查询")
    @RequiresPermissions(value = "admin:footprint:list")
    @GetMapping("/footprint/list")
    public Object footprintOfList(Integer page, Integer limit, String sort, String order, Integer userId, Integer goodsId) {
        IPage<DtsFootprint> iPage = dtsUserService.findFootprintListByQuery(page, limit, sort, order, userId, goodsId);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     *搜索历史之历史记录列表
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "搜索历史"}, button = "查询")
    @RequiresPermissions(value = "admin:history:list")
    @GetMapping("/history/list")
    public Object historyOfList(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        IPage<DtsSearchHistory> iPage = dtsUserService.findHistoryListByQuery(page, limit, sort, order, userId, keyword);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     *意见反馈之反馈记录列表
     */
    @RequiresPermissionsDesc(menu = {"用户管理", "意见反馈"}, button = "查询")
    @RequiresPermissions(value = "admin:feedback:list")
    @GetMapping("/feedback/list")
    public Object feedbackOfList(Integer page, Integer limit, String sort, String order, String username, Integer id) {
        IPage<DtsFeedback> iPage = dtsUserService.findFeedbackListByQuery(page, limit, sort, order, username, id);
        Map data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
